﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlTypes;

namespace EasyCLRBase
{
    public static class SqlTypeExtension
    {
        /// <summary>
        /// Cast SQL Object into the C# Object.
        /// </summary>
        /// <param name="val">incomming data to cast.</param>
        /// <returns>Return the "Value" Property of the Sql Type.</returns>
        public static Object GetVariantValue(this INullable val)
        {
            Object result = null;

            if (val != null)
            {
                if (val.IsNull == false)
                {
                    var t = val.GetType();
                    if (Type.Equals(t, typeof(SqlBinary)))
                        result = BitConverter.ToString(((SqlBinary)val).Value);
                    if (Type.Equals(t, typeof(SqlBoolean)))
                        result = ((SqlBoolean)val).Value;
                    if (Type.Equals(t, typeof(SqlByte)))
                        result = ((SqlByte)val).Value;
                    if (Type.Equals(t, typeof(SqlBytes)))
                        result = BitConverter.ToString(((SqlBytes)val).Value);
                    if (Type.Equals(t, typeof(SqlChars)))
                        result = new String(((SqlChars)val).Value);
                    if (Type.Equals(t, typeof(SqlDateTime)))
                        result = ((SqlDateTime)val).Value;
                    if (Type.Equals(t, typeof(SqlDecimal)))
                        result = ((SqlDecimal)val).Value;
                    if (Type.Equals(t, typeof(SqlDouble)))
                        result = ((SqlDouble)val).Value;
                    if (Type.Equals(t, typeof(SqlGuid)))
                        result = ((SqlGuid)val).Value;
                    if (Type.Equals(t, typeof(SqlInt16)))
                        result = ((SqlInt16)val).Value;
                    if (Type.Equals(t, typeof(SqlInt32)))
                        result = ((SqlInt32)val).Value;
                    if (Type.Equals(t, typeof(SqlInt64)))
                        result = ((SqlInt64)val).Value;
                    if (Type.Equals(t, typeof(SqlMoney)))
                        result = ((SqlMoney)val).Value;
                    if (Type.Equals(t, typeof(SqlSingle)))
                        result = ((SqlSingle)val).Value;
                    if (Type.Equals(t, typeof(SqlString)))
                        result = ((SqlString)val).Value;
                }
            }
            return result;
        }
        public static Boolean IsNullOrEmpty(this SqlString val)
        {
            return val.IsNull == true || String.IsNullOrEmpty(val.Value) == true;
        }
        public static Boolean IsNullOrEmpty(this SqlBinary val)
        {
            return val.IsNull == true || val.Length == 0;
        }

        public static String ValueOrEmpty(this String value)
        {
            return String.IsNullOrEmpty(value) == true ? String.Empty : value;
        }

        public static SqlString ToSqlValue(this String value)
        {
            var result = SqlString.Null;
            if (value != null)
                result = value;
            return result;
        }

        public static SqlDateTime ToSqlValue(this DateTime value)
        {
            var result = SqlDateTime.Null;

            if (value > SqlDateTime.MinValue || value < SqlDateTime.MaxValue)
                result = value;

            return result;
        }

        public static SqlBinary ToSqlValue(this Byte[] value)
        {
            return (value == null) ? SqlBinary.Null : value;
        }

        public static SqlChars ToSqlValue(this Char[] value)
        {
            return (value == null) ? SqlChars.Null : new SqlChars(value);
        }


        public static String ToNativeValue(this SqlString value)
        {
            return (value.IsNull == true) ? null : value.Value;
        }
    }
}
